package com.example.studentdemo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker   // 开启 STOMP
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws")          // 前端 SockJS 连接端点
                .setAllowedOriginPatterns("*")
                .withSockJS();
    }
    
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 订阅Broker名称前缀，客户端订阅时需要带上的前缀
        registry.enableSimpleBroker("/topic", "/queue");
        
        // 全局使用的消息前缀，客户端发送消息时需要带上的前缀
        registry.setApplicationDestinationPrefixes("/app");
        
        // 点对点使用的订阅前缀（客户端订阅地址），不设置的话，默认也是/user/
        registry.setUserDestinationPrefix("/user");
    }
}